﻿using Microsoft.Reporting.WebForms;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using ThoughtWorks.QRCode.Codec;
using YTBZ.PUB.COMMON;
using YTBZ.BUSINESSLOGIC;
using System.Configuration;

namespace YTBZ.REPORTING
{
    public partial class TXXDPRINT : System.Web.UI.Page
    {
        XDBLL bll = new XDBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string OrderBH = Request["OrderBH"];
                string PCNum = Request["PCNum"];
                if (Request["TXBH"] != null)
                {
                    string TXBH = Request["TXBH"].Trim();
                    proBindOne(OrderBH, PCNum, TXBH);//单箱打印
                }
                else 
                {
                    proBind(OrderBH, PCNum);//通过订单号、批次号批量打印
                }
            }
        }

        ////通过订单号、批次号批量打印
        public void proBind(string OrderBH, string PCNum)
        {
            LocalReport localReport = new LocalReport();
            ReportDataSource reportDataSource = new ReportDataSource();
            SortedList SDList = new SortedList();
            SDList["@OrderBH"] = OrderBH;
            SDList["@PCNum"] = PCNum;
            DataTable dt = bll.GetTX(SDList);

            String rptUrl = "~/REPORTING/TXXDLIST.rdlc";
            localReport.DataSources.Add(new ReportDataSource("PRINTZXLIST", dt));
            localReport.ReportPath = Server.MapPath(rptUrl);
            localReport.SubreportProcessing += new SubreportProcessingEventHandler(GOODOUTLOADReport_SubreportProcessing);
            localReport.Refresh();


            #region PDF打印
            string reportType = "PDF";
            string mimeType;
            string encoding;
            string fileNameExtension;

            //The DeviceInfo settings should be changed based on the reportType
            //http://msdn2.microsoft.com/en-us/library/ms155397.aspx
            string deviceInfo =
            "<DeviceInfo>" +
            "  <OutputFormat>PDF</OutputFormat>" +
            "  <PageWidth>210mm</PageWidth>" +
            "  <PageHeight>297mm</PageHeight>" +
            "  <MarginTop>2mm</MarginTop>" +
            "  <MarginLeft>2mm</MarginLeft>" +
            "  <MarginRight>2mm</MarginRight>" +
            "  <MarginBottom>2mm</MarginBottom>" +
            "</DeviceInfo>";

            Warning[] warnings;
            string[] streams;
            byte[] renderedBytes;

            //Render the report
            renderedBytes = localReport.Render(
                reportType,
                deviceInfo,
                out mimeType,
                out encoding,
                out fileNameExtension,
                out streams,
                out warnings);
            //Response.AddHeader("content-disposition", "attachment; filename=NorthWindCustomers." + fileNameExtension);
            Response.ContentType = "application/pdf";
            Response.BinaryWrite(renderedBytes);
            Response.Flush();
            Response.Close();
            #endregion
        }
        void GOODOUTLOADReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
        {
            List<ReportDataSource> rds = new List<ReportDataSource>();
            string OrderBH = e.Parameters["OrderBH"].Values[0];
            string PCNum = e.Parameters["PCNum"].Values[0];
            string TXBH = e.Parameters["TXBH"].Values[0];
            rds = GetSubreportData(OrderBH, PCNum, TXBH);
            e.DataSources.Add(rds[0]);
            e.DataSources.Add(rds[1]);
        }

        public List<ReportDataSource> proBindOne(string OrderBH, string PCNum, string TXBH)
        {
            List<ReportDataSource> rds = new List<ReportDataSource>();
            rds = GetSubreportData(OrderBH, PCNum, TXBH);

            String rptUrl = "~/REPORTING/TXXD.rdlc";
            LocalReport localReport = new LocalReport();
            localReport.ReportPath = Server.MapPath(rptUrl);
            localReport.DataSources.Add(rds[0]);
            localReport.DataSources.Add(rds[1]);

            #region PDF打印
            string reportType = "PDF";
            string mimeType;
            string encoding;
            string fileNameExtension;

            //The DeviceInfo settings should be changed based on the reportType
            //http://msdn2.microsoft.com/en-us/library/ms155397.aspx
            string deviceInfo =
            "<DeviceInfo>" +
            "  <OutputFormat>PDF</OutputFormat>" +
            "  <PageWidth>210mm</PageWidth>" +
            "  <PageHeight>297mm</PageHeight>" +
            "  <MarginTop>2mm</MarginTop>" +
            "  <MarginLeft>2mm</MarginLeft>" +
            "  <MarginRight>2mm</MarginRight>" +
            "  <MarginBottom>2mm</MarginBottom>" +
            "</DeviceInfo>";


            Warning[] warnings;
            string[] streams;
            byte[] renderedBytes;

            //Render the report
            renderedBytes = localReport.Render(
                reportType,
                deviceInfo,
                out mimeType,
                out encoding,
                out fileNameExtension,
                out streams,
                out warnings);
            //Response.AddHeader("content-disposition", "attachment; filename=NorthWindCustomers." + fileNameExtension);
            Response.ContentType = "application/pdf";
            Response.BinaryWrite(renderedBytes);
            Response.Flush();
            Response.Close();

            return rds;

            #endregion
        }

        //获取子报表绑定数据
        private List<ReportDataSource> GetSubreportData(string OrderBH, string PCNum, string TXBH)
        {
            List<ReportDataSource> rds = new List<ReportDataSource>();

            #region 数据绑定
            SortedList SDList = new SortedList();
            SDList["@OrderBH"] = OrderBH;
            SDList["@PCNum"] = PCNum;
            SDList["@TXBH"] = TXBH;
            DataTable dtKB = bll.GetXDBZXX(SDList);
            dtKB.Columns.Add("ROWNO");

            DataTable dtHead = dtKB.Clone();
            dtHead.Columns.Add("ERWEIMA");

            string FilePath = Server.MapPath(this.ResolveClientUrl("~/"));
            string strFilePath = FilePath;
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            for (int i = 0; i < dtKB.Rows.Count; i++)
            {
                if (i == 0)
                {
                    DataRow drHead = dtHead.NewRow();
                    #region 生成二维码图片
                    try
                    {
                        //string cururl = Request.Url.ToString();
                        string cururl = "http://" + Request.Url.Host + ":" + Request.Url.Port + "/Z11_XD/XDBZXXVIEW.aspx";
                        //二维码内容
                        string strBarCode = cururl + "?OrderBH=" + OrderBH + "&PCNum=" + PCNum + "&TXBH=" + TXBH;
                        //生成二维码图片 
                        strFilePath = BarCode.proGetBarCode(strBarCode, Guid.NewGuid().ToString(), 2, true, 120, 120, FilePath);
                        Byte[] fileDate = null;
                        SupFile m_file = new SupFile(strFilePath);
                        fileDate = m_file.GetFileByte();
                        drHead["ERWEIMA"] = Convert.ToBase64String(fileDate);
                    }
                    catch
                    {
                        strFilePath = "";
                    }
                    #endregion
                    drHead["OrderBH"] = dtKB.Rows[i]["OrderBH"];
                    drHead["PCNum"] = dtKB.Rows[i]["PCNum"];
                    drHead["CX"] = dtKB.Rows[i]["CX"];
                    drHead["PCS"] = dtKB.Rows[i]["PCS"];
                    drHead["TXBH"] = dtKB.Rows[i]["TXBH"];
                    drHead["TXCC"] = dtKB.Rows[i]["TXCC"];
                    drHead["TXJZ"] = dtKB.Rows[i]["TXJZ"];
                    drHead["TXMZ"] = dtKB.Rows[i]["TXMZ"];
                    dtHead.Rows.Add(drHead);
                }
                dtKB.Rows[i]["ROWNO"] = (i + 1).ToString();
            }
            #endregion

            rds.Add(new ReportDataSource("PRINTHEAD", dtHead));
            rds.Add(new ReportDataSource("PRINTITEM", dtKB));
            return rds;
        }


    }
}